package com.xctech.yace.swing.tab;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xctech.yace.service.FileService;
import com.xctech.yace.swing.ConfigValue;
import com.xctech.yace.swing.FatherSwing;
import com.xctech.yace.swing.InitFileConfig;
import com.xctech.yace.swing.component.ComboboxToolTipRenderer;
import com.xctech.yace.util.FormatUtil;
import com.xctech.yace.util.JSONUtil;
import com.xctech.yace.util.SelectUtil;
import org.apache.flink.api.java.tuple.Tuple2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Map;

import static javax.swing.JOptionPane.*;

public class ConversionSwing extends FatherSwing implements ConfigValue, InitFileConfig {

    public static String title = "json行列转换";

    public static JPanel genConvertPanel() {
        //转换画板
        JPanel convert = new JPanel();

        /*
         * 调用用户定义的方法并添加组件到面板
         */
        placeComponents(convert);

        return convert;
    }

    private static void placeComponents(JPanel panel) {
        /* 布局部分我们这边不多做介绍
         * 这边设置布局为 null
         */
        panel.setLayout(null);

        //增加模板
        JComboBox addModelBox=new JComboBox();    //创建JComboBox
        addModelBox.addItem("------------------------------------------------添加模板--------------------------------");    //向下拉列表中添加一项
        modelMapSql.entrySet().stream().forEach(model->{
            addModelBox.addItem(model.getKey());
        });
        addModelBox.setBounds(15,20,sfiledW+330,height);
        addModelBox.setToolTipText("直接选中即添加到到消息框中...");
        panel.add(addModelBox);

        ComboboxToolTipRenderer renderer = new ComboboxToolTipRenderer();
        addModelBox.setRenderer(renderer);


        // 生成按钮
        JButton genButton = new JButton("全部生成");
        genButton.setBounds(460,20,sfiledW,height);
        genButton.setToolTipText("全部生成");
        panel.add(genButton);


        //message按钮
        JTextArea convertFrom = new JTextArea();
        JScrollPane scrollArea = new JScrollPane(convertFrom);

        scrollArea.setMaximumSize(scrollArea.getPreferredSize());
        scrollArea.setBounds(15,20+height+space,800,870);
        scrollArea.setFont(new Font("Courier",Font.BOLD,12));

        scrollArea.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollArea.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        panel.add(scrollArea);

        //按钮 >>>
        JButton leftButton = new JButton(">>>");
        leftButton.setBounds(835,400,sfiledW/2,height);
        panel.add(leftButton);

        //按钮 <<<
        JButton rightButton = new JButton("<<<");
        rightButton.setBounds(835,450,sfiledW/2,height);
        panel.add(rightButton);

        //生成 >>>
        JButton genSqlButton = new JButton("G");
        genSqlButton.setBounds(835,500,sfiledW/2,height);
        panel.add(genSqlButton);

        //转换后message
        JTextArea convertTo = new JTextArea();
        JScrollPane scrollArea1 = new JScrollPane(convertTo);

        scrollArea1.setMaximumSize(scrollArea1.getPreferredSize());
        scrollArea1.setBounds(915,20+height+space,850,870);
        scrollArea1.setFont(new Font("Courier",Font.BOLD,12));

        scrollArea1.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollArea1.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        panel.add(scrollArea1);

        leftButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //清空右
                convertTo.setText("");
                String msg = convertFrom.getText().trim();
                JSONObject jsonObject = JSONObject.parseObject(msg);
                convertTo.setText(FormatUtil.format(jsonObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue)));
            }
        });

        rightButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //清空左
                convertFrom.setText("");
                String msg = convertTo.getText().trim();
                JSONObject jsonObject = JSONObject.parseObject(msg);
                convertFrom.setText(jsonObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue));
            }
        });

        genSqlButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                convertTo.setText("");
                String msg = convertFrom.getText().trim();
                JSONObject jsonObject = JSONObject.parseObject(msg);
                convertTo.setText(JSONUtil.gensql("TABLE",jsonObject));
            }
        });

        convertFrom.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_DELETE){
                    System.out.println("清空convertFrom界面");
                    convertFrom.setText("");
                }
            }
        });

        convertTo.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_DELETE){
                    System.out.println("清空convertTo界面");
                    convertTo.setText("");
                }
            }
        });

        //
        genButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fileName = String.valueOf(System.currentTimeMillis()) + ".sql";
                for(Map.Entry<String, String> entry:modelMapSql.entrySet()){
                    String tableName = entry.getKey();
                    String[] topics = tableName.split("#");
                    if(topics.length >= 2){
                        tableName = topics[1];
                    }
                    String sql = JSONUtil.gensql(tableName, JSONObject.parseObject(entry.getValue()));
                    FileService.saveSqlFile(fileName,sql+";");
                }
                JOptionPane.showMessageDialog(null, "成功", "成功", INFORMATION_MESSAGE);
            }
        });

        addModelBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    if(addModelBox.getSelectedIndex()!=0){
                        String modelKey = addModelBox.getSelectedItem().toString().trim();;
                        String[] msgs = modelMapSql.get(modelKey).split("#{5,}");
                        for(int i = 0;i < msgs.length;i++){
                            String msg = msgs[i];
                            JSONObject jsonObject = JSONObject.parseObject(msg);
                            convertFrom.append(FormatUtil.format(JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue))+"\n");
                        }
                    }
                }
            }
        });
    }


}
